package cn.mothin.action.system;

import org.springframework.beans.factory.annotation.Autowired;

import cn.mothin.frame.bean.ActionResult;
import cn.mothin.frame.core.annotation.Module;
import cn.mothin.frame.core.annotation.NeedLogon;
import cn.mothin.frame.core.annotation.NeedModule;
import cn.mothin.frame.core.annotation.NeedPermission;
import cn.mothin.frame.core.annotation.NeedPermissionAs;
import cn.mothin.frame.core.annotation.NeedPermissionTemplate;
import cn.mothin.frame.core.annotation.NeedSuper;
import cn.mothin.frame.core.annotation.OperationLink;
import cn.mothin.frame.core.base.BaseAction;
import cn.mothin.service.system.RoleService;
import cn.mothin.vo.system.RoleForm;

@SuppressWarnings("serial")
@Module(id = "5404de9e7b0b2b5f75bfd72f", name = "角色管理")
public class RoleAction extends BaseAction {
    private RoleForm roleForm = new RoleForm();
    @Autowired
    private RoleService roleService;

    @Override
    public Object getModel() {
        return roleForm;
    }
    
    /**
     * 获取角色信息，用于设置或者更新人员角色
     * @return
     */
    @NeedLogon
    public String queryUserRole(){
        return this.renderJson(roleService.queryRoleJson(roleForm));
    }

    /**
     * 角色管理主页面
     */
    @NeedModule
    public String execute() {
        return SUCCESS;
    }

    /**
     * 查询角色列表信息
     * 
     * @return
     */
    @NeedModule
    public String query() {
        return this.renderJson(roleService.query(roleForm));
    }

    /**
     * 添加新角色页面
     * 
     * @return
     */
    @OperationLink(link = "execute")
    @NeedPermissionTemplate(id = "5404df197b0b2b5f75bfd730", template = NeedPermissionTemplate.CREATE)
    public String add() {
        return "add";
    }

    /**
     * 保存新角色
     * 
     * @return
     */
    @NeedPermissionAs("add")
    public String saveForAdd() {
        return renderJson(roleService.addSystemRole(roleForm).toJson());
    }

    @OperationLink(link = "execute", ordinal = 2, rowOperation = true)
    @NeedPermissionTemplate(id = "5404dffe7b0b2b5f75bfd731", template = NeedPermissionTemplate.UPDATE)
    public String update() {
        int result = roleService.queryRoleById(roleForm);
        if (result == 0) {
            return "update";
        } else if (result == 1) {
            setLastError("指定的角色不存在！");
        } else if (result == 2) {
            setLastError("此角色已被删除！");
        }
        return ERROR;
    }

    @NeedPermissionAs("update")
    public String saveForUpdate() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = roleService.update(roleForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的角色不存在！");
        } else if (result == 2) {
            actionResult.setOk(false);
            actionResult.setMessage("此角色已被删除！");
        } else if (result == 3) {
            actionResult.setOk(false);
            actionResult.setMessage("角色名称已存在！");
        }
        return renderJson(actionResult.toJson());
    }

    @OperationLink(link = "execute", ordinal = 3, rowOperation = true)
    @NeedPermissionTemplate(id = "5404e0527b0b2b5f75bfd732", template = NeedPermissionTemplate.DELETE)
    public String delete() {
        ActionResult actionResult = ActionResult.getInstance();
        int result = roleService.delete(roleForm);
        if (result == 1) {
            actionResult.setOk(false);
            actionResult.setMessage("指定的角色不存在！");
        } else if (result == 2) {
            actionResult.setOk(false);
            actionResult.setMessage("此角色已被删除！");
        }
        return renderJson(actionResult.toJson());
    }

    @OperationLink(link = "execute", ordinal = 0, rowOperation = true)
    @NeedPermission(id = "5404e0c97b0b2b5f75bfd733", name = "查看权限", ico = "glyphicon glyphicon-list-alt")
    public String permission() {
        return "view_permission";
    }

    @NeedPermissionAs("permission")
    public String queryPermission() {
        return renderJson(toJson(roleService.queryPermission(roleForm)));
    }

    @NeedSuper
    public String configPermission() {
        return "config_permission";
    }

    @NeedSuper
    public String queryPermissionForConfig() {
        ActionResult actionResult = ActionResult.getInstance();
        actionResult.setValue(roleService.queryPermissionForUpdate(roleForm));
        return renderJson(actionResult.toJson());
    }

    @NeedSuper
    public String saveForConfigPermission() {
        roleService.saveForConfigPermission(roleForm);
        return renderJson(ActionResult.getInstance().toJson());
    }
}
